import django.conf as conf
import django.contrib.syndication.views as sv
import django.contrib.sites.models as sm
import django.core.exceptions as exc
import django.core.urlresolvers as ur
import django.http as http
import django.utils.feedgenerator as fg
import ourcity.core.models as cm
import ourcity.news.models as nm
import ourcity.reps.models as rm

CURRENT_SITE = sm.Site.objects.get_current()

class News(sv.Feed):
    feed_type = fg.Atom1Feed
    title = '%s News' % CURRENT_SITE.name
    link = '/'

    def get_object(self, request, user_id=None):
        # 404 for now
        raise http.Http404
        if user_id:
            try:
                return cm.Profile.objects.get(user=user_id)
            except cm.Profile.DoesNotExist:
                return None

    def item_author_link(self, item):
        return 'http://%s%s' % (CURRENT_SITE.domain, 
                item.author.get_absolute_url())

    def item_author_name(self, item):
        return item.get_author()

    def item_categories(self, item):
        return item.issues.values_list('name', flat=True)

    def item_pubdate(self, item):
        return item.pub_date

    def items(self, obj):
        if obj:
            districts = obj.districts.all()
            reps = rm.Representative.objects.active().filter(
                    district__in=districts)
            news = nm.News.objects.active().filter(reps__in=reps).distinct()
            return news[:10]
        else:
            return nm.News.objects.active()[:10]

#class LatestEntriesFeed(Feed):
#    title = "Chicagocrime.org site news"
#    link = "/sitenews/"
#    description = "Updates on changes and additions to chicagocrime.org."
#
#    def items(self):
#        return NewsItem.objects.order_by('-pub_date')[:5]
#
#    def item_title(self, item):
#        return item.title
#
#    def item_description(self, item):
#        return item.description

class Posts(sv.Feed):
    feed_type = fg.Atom1Feed
    title = '%s Blog Posts' % CURRENT_SITE.name
    link = '/blog'

    def items(self):
        return nm.News.objects.posts()[:10]

    def item_author_link(self, item):
        return 'http://%s%s' % (CURRENT_SITE.domain, 
                item.author.get_absolute_url())

    def item_author_name(self, item):
        return item.get_author()

    def item_categories(self, item):
        return item.issues.values_list('name', flat=True)

    def item_pubdate(self, item):
        return item.pub_date

class Reports(sv.Feed):
    feed_type = fg.Atom1Feed
    title = '%s Reports' % CURRENT_SITE.name
    link = '/reports'

    def items(self):
        return nm.News.objects.reports()[:10]

    def item_author_link(self, item):
        return 'http://%s%s' % (CURRENT_SITE.domain, 
                item.author.get_absolute_url())

    def item_author_name(self, item):
        return item.get_author()

    def item_categories(self, item):
        return item.issues.values_list('name', flat=True)

    def item_pubdate(self, item):
        return item.pub_date

